package com.welson.restfultoolx.ui;

import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.util.ui.JBUI;
import com.welson.restfultoolx.model.ApiInfo;
import com.welson.restfultoolx.model.ParamInfo;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * API详情面板
 * 显示选中API的详细信息
 */
public class ApiDetailsPanel extends JPanel {
    
    private final ApiInfo apiInfo;
    
    public ApiDetailsPanel(ApiInfo apiInfo) {
        this.apiInfo = apiInfo;
        setLayout(new BorderLayout());
        setBorder(JBUI.Borders.empty(5));
        
        initComponents();
    }
    
    private void initComponents() {
        // 创建主内容面板
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
        
        // 添加基本信息
        addBasicInfo(contentPanel);
        
        // 添加参数信息
        addParameterInfo(contentPanel);
        
        // 添加其他信息
        addAdditionalInfo(contentPanel);
        
        // 使用滚动面板包装内容
        JBScrollPane scrollPane = new JBScrollPane(contentPanel);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setBorder(null);
        
        add(scrollPane, BorderLayout.CENTER);
    }
    
    private void addBasicInfo(JPanel parent) {
        // HTTP方法和路径
        JPanel methodPanel = createInfoRow("Method:", 
            String.format("%s %s", apiInfo.getHttpMethod(), apiInfo.getPath()));
        parent.add(methodPanel);
        parent.add(Box.createVerticalStrut(5));
        
        // 类名
        if (apiInfo.getClassName() != null) {
            JPanel classPanel = createInfoRow("Class:", apiInfo.getClassName());
            parent.add(classPanel);
            parent.add(Box.createVerticalStrut(5));
        }
        
        // 方法名
        if (apiInfo.getMethodName() != null) {
            JPanel methodNamePanel = createInfoRow("Method Name:", apiInfo.getMethodName());
            parent.add(methodNamePanel);
            parent.add(Box.createVerticalStrut(5));
        }
        
        // 返回类型
        if (apiInfo.getReturnType() != null) {
            JPanel returnPanel = createInfoRow("Return Type:", getSimpleTypeName(apiInfo.getReturnType()));
            parent.add(returnPanel);
            parent.add(Box.createVerticalStrut(10));
        }
    }
    
    private void addParameterInfo(JPanel parent) {
        List<ParamInfo> parameters = apiInfo.getParameters();
        if (parameters == null || parameters.isEmpty()) {
            return;
        }
        
        // 参数标题
        JBLabel paramTitle = new JBLabel("Parameters:");
        paramTitle.setFont(paramTitle.getFont().deriveFont(Font.BOLD));
        parent.add(paramTitle);
        parent.add(Box.createVerticalStrut(5));
        
        // 参数列表
        for (ParamInfo param : parameters) {
            JPanel paramPanel = createParameterPanel(param);
            parent.add(paramPanel);
            parent.add(Box.createVerticalStrut(3));
        }
        
        parent.add(Box.createVerticalStrut(10));
    }
    
    private void addAdditionalInfo(JPanel parent) {
        // 注解信息
        List<String> annotations = apiInfo.getAnnotations();
        if (annotations != null && !annotations.isEmpty()) {
            JBLabel annotationTitle = new JBLabel("Annotations:");
            annotationTitle.setFont(annotationTitle.getFont().deriveFont(Font.BOLD));
            parent.add(annotationTitle);
            parent.add(Box.createVerticalStrut(5));
            
            for (String annotation : annotations) {
                JBLabel annotationLabel = new JBLabel("@" + annotation);
                annotationLabel.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 11));
                annotationLabel.setForeground(new Color(128, 128, 128));
                parent.add(annotationLabel);
            }
            
            parent.add(Box.createVerticalStrut(10));
        }
        
        // 文件路径
        if (apiInfo.getFilePath() != null && !apiInfo.getFilePath().isEmpty()) {
            JPanel filePanel = createInfoRow("File:", getSimpleFileName(apiInfo.getFilePath()));
            parent.add(filePanel);
        }
    }
    
    private JPanel createInfoRow(String label, String value) {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 20));
        
        JBLabel labelComponent = new JBLabel(label);
        labelComponent.setFont(labelComponent.getFont().deriveFont(Font.BOLD));
        labelComponent.setPreferredSize(new Dimension(80, 20));
        
        JBLabel valueComponent = new JBLabel(value);
        valueComponent.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 11));
        
        panel.add(labelComponent, BorderLayout.WEST);
        panel.add(valueComponent, BorderLayout.CENTER);
        
        return panel;
    }
    
    private JPanel createParameterPanel(ParamInfo param) {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));
        panel.setBorder(JBUI.Borders.emptyLeft(10));
        
        // 参数名和类型
        String paramText = String.format("%s: %s", 
            param.getName(), getSimpleTypeName(param.getType()));
        
        JBLabel paramLabel = new JBLabel(paramText);
        paramLabel.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 11));
        
        panel.add(paramLabel, BorderLayout.WEST);
        
        // 参数属性（注解、必需等）
        StringBuilder attributes = new StringBuilder();
        if (param.getAnnotation() != null && !param.getAnnotation().isEmpty()) {
            attributes.append("@").append(param.getAnnotation());
        }
        if (param.isRequired()) {
            if (attributes.length() > 0) {
                attributes.append(", ");
            }
            attributes.append("required");
        }
        if (param.getDefaultValue() != null && !param.getDefaultValue().isEmpty()) {
            if (attributes.length() > 0) {
                attributes.append(", ");
            }
            attributes.append("default: ").append(param.getDefaultValue());
        }
        
        if (attributes.length() > 0) {
            JBLabel attrLabel = new JBLabel(" (" + attributes.toString() + ")");
            attrLabel.setFont(attrLabel.getFont().deriveFont(Font.ITALIC, 10f));
            attrLabel.setForeground(Color.GRAY);
            panel.add(attrLabel, BorderLayout.CENTER);
        }
        
        return panel;
    }
    
    private String getSimpleTypeName(String fullTypeName) {
        if (fullTypeName == null || fullTypeName.isEmpty()) {
            return "";
        }
        
        // 移除泛型信息
        int genericIndex = fullTypeName.indexOf('<');
        if (genericIndex > 0) {
            fullTypeName = fullTypeName.substring(0, genericIndex);
        }
        
        // 获取简单类名
        int lastDot = fullTypeName.lastIndexOf('.');
        return lastDot >= 0 ? fullTypeName.substring(lastDot + 1) : fullTypeName;
    }
    
    private String getSimpleFileName(String fullPath) {
        if (fullPath == null || fullPath.isEmpty()) {
            return "";
        }
        
        int lastSeparator = Math.max(fullPath.lastIndexOf('/'), fullPath.lastIndexOf('\\'));
        return lastSeparator >= 0 ? fullPath.substring(lastSeparator + 1) : fullPath;
    }
}